package com.beiding.v2.test.pg;

import com.beiding.v2.base.IContext;
import com.beiding.v2.base.IUnit;
import com.beiding.v2.base.IUnitHandler;
import com.beiding.v2.element.IField;
import com.beiding.v2.element.IFiledCapture;
import com.beiding.v2.element.ITable;

public class FieldHandler implements IUnitHandler<IFiledCapture> {
    @Override
    public boolean canHandle(IUnit unit) {
        return unit instanceof IFiledCapture;
    }

    @Override
    public String handle(IContext context, IFiledCapture unit) {
        IField field = unit.getField();

        String r;

        ITable table = field.getTable();
        if (table.getAlias() == null) {
            r = field.getName();
        } else {
            r = table.getAlias() + "." + field.getName();
        }

        if (unit.getFunctionType() != null) {
            r= "\"" + unit.getFunctionType().getText() + "\"(" + r + ")";
        }

        if (unit.getAs() != null) {
            r = r + " as " + unit.getAs();
        }

        return r;
    }
}
